package com.freedy.concurrent.OptimisticLock;

import java.lang.ref.SoftReference;
import java.math.BigInteger;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;

/**
 * @author Freedy
 * @date 2021/4/23 21:57
 */
public class AtomicIntegerTest {
    public static void main(String[] args) {
        AtomicInteger i=new AtomicInteger();
        i.incrementAndGet();//++i
        i.getAndIncrement();//i++
        i.addAndGet(2);//i+=2
        i.getAndAdd(2);
        i.updateAndGet(ope-> ope*5);
    }

    public static void updateAndGet(AtomicInteger i, Function<Integer,Integer> function){
        for (;;){
            int prev=i.get();
            int next=function.apply(prev);
            if (i.compareAndSet(prev,next))
                break;
        }
    }


}
